package main // 声明main包，表明当前是一个可执行程序

import (
	"fmt"
	"strings"
) // 导入内置fmt包

var a string

const (
	n1 = 100
	n2
	n3
) // 常量n1、n2、n3的值都是100

const (
	c1, c2 = iota, iota + 1     // 0,1
	c3, c4 = iota + 2, iota + 3 // 3,4
)

func ifDemo2() {
	if score := 65; score >= 90 {
		fmt.Println("A")
	} else if score > 75 {
		fmt.Println("B")
	} else {
		fmt.Println("C")
	}
	// fmt.Println(score)  这里的score为if判断的局部变量，所以不能访问
}

func sliceDemo() {
	var a []int
	a = append(a, 1)       // [1]
	a = append(a, 2, 3, 4) // [1 2 3 4]
	a1 := []int{5, 6, 7}
	a = append(a, a1...)
	fmt.Println(a) // [1 2 3 4 5 6 7]
	a = append(a, 8, 9, 10)
	fmt.Println(a) // [1 2 3 4 5 6 7 8 9 10]

	// 修改切片影响底层数组
	s1 := [...]int{1, 3, 5, 7, 9, 11, 13, 15}
	s2 := s1[:]
	// 删除索引为1的那个3
	s2 = append(s2[0:1], s2[2:]...)
	fmt.Println(s2) // [1 5 7 9 11 13 15]
	fmt.Println(s1) // [1 5 7 9 11 13 15 15]  底层数组原长度不变，[3, 5, 7, 9, 11, 13] => [5 7 9 11 13 15]

	var s3 []int            //没有分配内存 nil
	fmt.Println(s3 == nil)  // true
	s4 := make([]int, 0, 4) // make分配了内存
	fmt.Println(s4 == nil)  // false
}

func mapDemo() {
	m1 := make(map[string]int, 10)
	m1["张三"] = 18
	m1["李四"] = 41
	fmt.Println(m1)
	fmt.Printf("type of m1:%T\n", m1)
}

type NewInt int
type MyInt = int

type person struct {
	name string
	city string
	age  int8
}

func main() { // main函数，程序执行入口
	fmt.Println("Hello World!")
	var heihei string
	heihei = "123"
	fmt.Println(heihei)
	fmt.Println(n3)

	fmt.Println(c1)
	fmt.Println(c2)
	fmt.Println(c3)
	fmt.Println(c4)

	num1 := 100
	num2 := int8(33)
	fmt.Printf("%T\n", num1) // int
	fmt.Printf("%T\n", num2) // int8

	str := "hello"
	fmt.Println(len(str))                 // 5
	fmt.Printf("你好：len(%d)\n", len("你好")) // 6
	str2 := "world"
	str3 := str + str2
	fmt.Println(str3) // helloworld
	str4 := strings.Split(str3, "o")
	fmt.Printf("\n%v", str4)                          // [hell w rld]
	fmt.Printf("\n%v", strings.Contains(str3, "lo"))  // true
	fmt.Printf("\n%v", strings.HasPrefix(str3, "el")) // false
	fmt.Printf("\n%v", strings.HasSuffix(str3, "ld")) // true
	fmt.Printf("\n%v", strings.Index(str3, "o"))      // 4
	fmt.Printf("\n%v", strings.LastIndex(str3, "o"))  // 6
	fmt.Printf("\n%s", strings.Join(str4, ","))       // hell,w,rld

	ifDemo2()

	//  for range 循环
	for index, value := range str {
		fmt.Printf("%d, %c\n", index, value)
	}

	sliceDemo()
	mapDemo()

	var a NewInt
	var b MyInt

	fmt.Printf("type of a:%T\n", a) // type of a:main.NewInt
	fmt.Printf("type of b:%T\n", b) // type of b:int

	var user struct {
		Name string
		Age  int
	}
	user.Name = "小王子"
	user.Age = 18
	fmt.Println(user) // {小王子 18}

	var p2 = new(person)
	fmt.Printf("%T\n", p2)     // *main.person
	fmt.Printf("p2=%#v\n", p2) // p2=&main.person{name:"",city:"",age:0}

	p3 := &person{}
	fmt.Printf("%T\n", p3)     // *main.person
	fmt.Printf("p3=%#v\n", p3) // p3=&main.person{name:"", city:"", age:0}
	p3.name = "七米"
	p3.age = 30
	p3.city = "成都"
	fmt.Printf("p3=%#v\n", p3) // p3=&main.person{name:"七米", city:"成都", age:30}
}
